<template>
  <common-list>
    <el-form slot="search" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型">
          <el-option label="全部" :value="null"/>
          <el-option label="系统" value="1"/>
          <el-option label="告警" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item label="IP" prop="ip">
        <common-input
          v-model="queryParams.ip"
          placeholder="请输入IP"
          clearable

          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row slot="tools" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['config:white:add']"
        >新增
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['config:white:remove']"
        >批量删除
        </el-button>
      </el-col>
    </el-row>

    <common-table slot="content" v-loading="loading" :data="whiteList" @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="类型" align="left" prop="type">
        <template slot-scope="{row}">
          <span v-if="row.type == 1">系统</span>
          <span v-if="row.type == 2">告警</span>
        </template>
      </el-table-column>
      <el-table-column label="IP" align="left" prop="ip"/>
      <el-table-column label="厂商编码" align="left" prop="manuCode"/>
      <el-table-column label="系统编码" align="left" prop="systemCode"/>
      <el-table-column label="描述" align="left" prop="description"/>
      <el-table-column label="操作" align="left" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button

            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['config:white:edit']"
          >编辑
          </el-button>
          <el-button

            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['config:white:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getWhite"
    />

    <common-edit-dialog :title="title" :visible.sync="open" @cancel="cancel" @submit="submitForm" :button-loading="isPersistenceLoading">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px"  >
        <!--<el-form-item label="资产ID" prop="assetId">
          <common-input v-model="form.assetId" placeholder="请输入资产ID"/>
        </el-form-item>-->
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" filterable
                     clearable>
            <el-option label="系统" value="1"/>
            <el-option label="告警" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="IP" prop="ip">
          <common-input v-model="form.ip" placeholder="请输入ip"/>
        </el-form-item>
        <el-form-item label="厂商编码" prop="manuCode">
          <common-input v-model="form.manuCode" :type="'text'" placeholder="请输入厂商编码"/>
        </el-form-item>
        <el-form-item label="系统编码" prop="systemCode">
          <common-input v-model="form.systemCode" :type="'text'" placeholder="请输入系统编码"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <common-input v-model="form.description" placeholder="请输入描述"/>
        </el-form-item>
      </el-form>
    </common-edit-dialog>

  </common-list>
</template>

<script>
import {
  listWhite,
  getWhite,
  delWhite,
  addWhite,
  updateWhite,
} from "@/api/system/white/white";

export default {
  name: "White",
  components: {},
  data() {
    return {
      exportLoading: false,
      isPersistenceLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示查询条件
      showSearch: true,
      // 总条数
      total: 0,
      // 白名单管理表格数据
      whiteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assetId: null,
        ip: null,
        type: null,
        description: null,
        manuCode: null,
        systemCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        manuCode: [
          { required: true, message: "请输入厂商编码", trigger: "blur" },
        ],
        systemCode: [
          { required: true, message: "请输入系统编码", trigger: "blur" },
        ],
        ip: [{ required: true, message: "请输入ip", trigger: "blur" }],
        type: [{ required: true, message: "请选择类型", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getWhite();
  },
  methods: {
    /** 查询白名单管理列表 */
    getWhite() {
      this.loading = true;
      listWhite(this.queryParams).then((response) => {
        this.whiteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        assetId: null,
        ip: null,
        type: null,
        description: null,
        manuCode: null,
        systemCode: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
      };
      this.resetForm("form");
    },
    /** 查询按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getWhite();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 编辑按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWhite(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            if (!this.isPersistenceLoading) {
              this.isPersistenceLoading = true;
              updateWhite(this.form)
                .then((response) => {
                  if (response && response.code === 200) {
                    console.log(response);
                    this.$GlobalSuccessMessage("编辑成功");
                    this.open = false;
                    this.getWhite();
                  } else {
                    this.$GlobalErrorConfirm("编辑失败", response.msg);
                  }
                })
                .finally(() => {
                  this.isPersistenceLoading = false;
                });
            }
          } else {
            if (!this.isPersistenceLoading) {
              this.isPersistenceLoading = true;
              addWhite(this.form)
                .then((response) => {
                  if(response && response.code === 200) {
                    this.$GlobalSuccessMessage("新增成功");
                    this.open = false;
                    this.getWhite();
                  }else{
                      this.$GlobalErrorConfirm('新增失败',response.msg)
                  }
                })
                .finally(() => {
                  this.isPersistenceLoading = false;
                });
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids.join(",");

      this.$GlobalDeleteConfirm().then(() => {
        delWhite({ ids }).then(() => {
          this.getWhite();
          this.$GlobalSuccessMessage("删除成功");
        });
      }).catch(()=>{});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.exportLoading = true;
      this.download(
        "system/whiteList/export",
        {
          ...this.queryParams,
          pageSize: 10000,
        },
        `白名单.xlsx`
      ).finally(()=>{
        this.exportLoading = false;
      });
    },
  },
};
</script>
